<?
require_once('functions.php');

function GetTheorists() {
	$connect = GetConnection();
	return $connect->query('SELECT * FROM Psi_Theorists');
}
function GetTheorist($id) {
	$connect = GetConnection();
	$sql = "SELECT * FROM Psi_Theorists WHERE id=$id";
	$results = $connect->query($sql);
	//echo $sql; die();
	$row = $results->fetch_assoc();
	$connect->close();
	return $row;
}
function BlankTheorist()
{
	return array('Name'=>null,'Born'=>null,'Died'=>null,'FamilyLife'=>null,'Acomplishments'=>null,'Biography'=>null,'created_at'=>null,'updated_at'=>null,'id'=>null);
}
function SaveTheorist($row) {
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	$sql = 	"UPDATE Psi_Theorists "
		.	"Set "
		
		.	"Name='$escape[Name]', "
		.	"Born='$escape[Born]', "
		.	"Died='$escape[Died]', "
		.	"FamilyLife='$escape[FamilyLife]', "
		.	"Acomplishments='$escape[Acomplishments]', "
		.	"Biography='$escape[Biography]' "
		
		.	"WHERE id=$row[id] ";
	$results = $connect->query($sql);
	//echo $sql;
	$error = $connect->error;
	$connect->close();
	
	return $error == '' ? TRUE : array('SQL Error' => $error);
}
function CreateTheorist($row) {
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	
	$Name 				= 	$_POST['Name'];
	$Born 				= 	$_POST['Born'];
	$Died 				= 	$_POST['Died'];
	$FamilyLife 		= 	$_POST['FamilyLife'];
	$Acomplishments 	= 	$_POST['Acomplishments'];
	$Biography			= 	$_POST['Biography'];
	
	$sql = 
		"INSERT INTO Psi_Theorists (
			Name, Born, Died, FamilyLife, Acomplishments, Biography, created_at
		) VALUES (
			'$escape[Name]', '$escape[Born]', '$escape[Died]', '$escape[FamilyLife]', '$escape[Acomplishments]', '$escape[Biography]', NOW() 
		)";
	
	$result = $connect->query($sql);
	if ($result) {
		// Success!
		header("Location: index.php");
		die();
	} else {
		// Display error message.
		echo "<p class=\"error\">Subject creation failed.</p>";
		echo "<p class=\"error\">" . mysql_error() . "</p>";
	}

	$error = $connect->error;
	$connect->close();
	
	return $error == '' ? TRUE : array('SQL Error' => $error);
}
function DeleteTheorist($id)
{
	$conn = GetConnection();
	$sql = 	"DELETE FROM Psi_Theorists WHERE id=$id";
	$results = $conn->query($sql);
	//echo $sql;
	$error = $conn->error;
	$conn->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}
/*function DeleteTheorist($row) {
	$connect = GetConnection();

	if (intval($_GET['id']) == 0) {
			header("Location: index.php");
			die();
	}
	
	$id = mysql_prep($_GET['id']);
	
	if ($subject = GetTheorist($id)) {
		
		$sql = "DELETE FROM Psi_Theorists WHERE id = {$id} LIMIT 1";
		$result = $connect->query($sql);
		if ( $connect->affected_rows == 1 ) {
			header("Location: index.php");
			die();
		} else {
			// Deletion Failed
			echo "<p class=\"error\">Subject deletion failed.</p>";
			echo "<p class=\"error\">" . mysql_error() . "</p>";
			echo "<a href=\"index.php\">Return to Main Page</a>";
		}
	} else {
		// Theorist didn't exist in database
			header("Location: index.php");
			die();
	}

	$connect->close();
}*/
function ValidateTheorist($row)
{
	$errors = array();
	if(empty($row['Name'])) $errors['Name'] = 'Name is required';
	if(empty($row['Born'])) $errors['Born'] = 'Birthday is required';

	return count($errors) == 0 ? true : $errors;
}